package httputil;

import com.gx.util.httpclientutil.HttpClientUtil;
import com.gx.util.httpclientutil.common.HttpConfig;
import com.gx.util.httpclientutil.exception.HttpProcessException;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.message.BasicHeader;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gaoxing
 * @create 2017-10-30 下午6:01
 */
@Slf4j
public class KaShenDemo {
    private static final String pattern = "<div id=\"main_messaqge(.*?)\">"; //()必须加，
    private static final Pattern r = Pattern.compile(pattern, Pattern.MULTILINE);// Pattern.MULTILINE 好像没有什么用，所以才使用上面的replace

    public static void main(String[] args) throws HttpProcessException {
        //登录地址
        String loginUrl = "http://www.kashen.com";
        String loginform = "https://www.kashen.com/member.php?mod=logging&action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=fwin_content_login/";
        String postForm = "https://www.kashen.com/member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&loginhash=%s&inajax=1";
        //登录首页
        HttpClientContext context = new HttpClientContext();
        CookieStore cookieStore = new BasicCookieStore();
        context.setCookieStore(cookieStore);
        HttpConfig config =HttpConfig.custom().headers(headers()).url(loginUrl).context(context);


        //获取参数
        String index = HttpClientUtil.get(config);//可以用.send(config)代替，但是推荐使用明确的get方法
        Document document = Jsoup.parse(index);

        Elements elements = document.select("input:nth-child(2)");
        String formhash = elements.attr("value");
        log.info("formhash={}",formhash);


        //获取登录框
        config.url(loginform);
        String form = HttpClientUtil.options(config);//可以用.send(config)代替，但是推荐使用明确的get方法
        Matcher m = r.matcher(form);
        String headhash = null;
        if (m.find()) {
            headhash = m.group();
            headhash = headhash.replace("<div id=\"main_messaqge_", "");
            headhash = headhash.replace("\">", "");
        }
        log.info("headhash={}",headhash);
        HashMap<String,Object> formdata = new HashMap<>();
        formdata.put("referer","https://www.kashen.com/forum.php");
        formdata.put("loginfield","username");
        formdata.put("username","zaqxsw123");
        formdata.put("password","123456");
        formdata.put("questionid","0");
        formdata.put("answer","");
        formdata.put("formhash",formhash);
        config.url(String.format(postForm,headhash)).map(formdata).formdata();
        String ret = HttpClientUtil.post(config);//可以用.send(config)代替，但是推荐使用明确的get方法
        System.out.println(ret);
    }
    private static List<Header> headerList() {
        List<Header> headerList = new ArrayList<>();
        Header userAgent = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36");
        headerList.add(userAgent);
        Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate");
        Header acceptLanguage = new BasicHeader("Accept-Language", "zh-CN");
        headerList.add(acceptEncoding);
        headerList.add(acceptLanguage);
        return headerList;
    }
    private static Header[] headers() {
        Header [] headers = new Header[3];
        Header userAgent = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36");
        headers[0]=(userAgent);
        Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate");
        Header acceptLanguage = new BasicHeader("Accept-Language", "zh-CN");
        headers[1]=(acceptEncoding);
        headers[2]= (acceptLanguage);
        return headers;
    }

}
